<template>
  <div v-if="reviewType" class="review-wrap">
    <div class="review-head" v-if="createBy">
      <h2>评论</h2>
      <el-button
        v-show="!showBtn"
        type="primary"
        size="small"
        icon="el-icon-edit"
        class="add-btn"
        @click="addEvaluate()"
      >
        我要发布
      </el-button>
    </div>
    <div class="evaluate-ul">
      <el-empty v-show="!tableData.length" description="暂无数据"></el-empty>
      <List v-if="tableData.length" :tableData="tableData" />
      <Pagination
        v-show="parseInt(pageObj.count) > 0"
        :total="parseInt(pageObj.count)"
        @change="getList"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
      />
    </div>
    <Add :resType="listQuery.resType" :resId="listQuery.resId" />
  </div>
</template>

<script>
import { openApiSysEvaluateQuery } from '@/api/openApi'
import Bus from '@/utils/bus.js'
import setInfo from '@/mixins/setInfo'
export default {
  mixins: [setInfo],
  components: {
    Add: () => import('./Add.vue'),
    List: () => import('./List.vue'),
  },
  data() {
    return {
      createBy: this.$store.getters.userId,
      reviewType: false,
      listQuery: {
        page: 1,
        limit: 10,
        resId: '',
        type: 1,
        resType: '',
      },
      tableData: [],
      pageObj: {},
    }
  },
  props: ['reviewObj', 'showBtn'],
  mounted() {
    this.getReviewType()
    Object.assign(this.listQuery, this.reviewObj)
    this.getList()
    Bus.$on('refreshReviewList', (v) => {
      if (v) {
        Object.assign(this.listQuery, v)
      }
      this.getList()
    })
  },
  beforeDestroy() {
    Bus.$off('refreshReviewList')
  },
  methods: {
    //评论开关
    async getReviewType() {
      let res = await this.$store.dispatch('getDictionaryVal', 'REVIEW_SWITCH')
      this.reviewType = res[0] == 1
      if (this.reviewType) {
        this.getList()
      }
    },
    getList() {
      if (!this.listQuery.resId) return
      openApiSysEvaluateQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data
        this.tableData.forEach((v) => {
          v.score = parseInt(v.score)
        })
        this.pageObj = res.data
      })
    },
    addEvaluate() {
      // 预览模式不触发事件
      if (this.$route.query.queryMode == 2) {
        this.previewTip()
        return
      }
      //判断是否登录
      if (!this.$store.getters.userId) {
        this.loginTip()
        return
      }
      Bus.$emit('showDialogReviewAdd')
    },
  },
}
</script>
<style scoped lang="scss" >
.review-wrap {
  .review-head {
    position: relative;
    padding-bottom: 20px;
    border-bottom: 1px solid #f5f5f5;
    h2 {
      font-size: 20px;
      color: #333;
    }
    .add-btn {
      position: absolute;
      right: 0;
      top: 0;
    }
  }
  .evaluate-ul {
    padding-top: 10px;
  }
}
</style>
